#include"address_list.h"
void menu()
{
	printf("------------function-----------\n");
	printf("        1：add\n");
	printf("        2：del\n");
	printf("        3：change\n");
	printf("        4：search\n");
	printf("        5：sort\n");
	printf("        6：display\n");
	printf("        0：exit program\n");
	printf("-------------------------------\n");
}
void InitaAddressList(AddressList* ps)
{
	assert(ps);
	ps->count = 0;
	memset(ps->date, 0, sizeof(ps->date));
}
void Add_AddressList_info(AddressList* ps)
{
	 assert(ps);
	if (ps->count <= COUNT)
	{
		printf("请输入名字:");
		scanf("%s",ps->date[ps->count].name);
		printf("请输入年龄:");
		scanf("%d", &(ps->date[ps->count].age));
		printf("请输入性别:");
		scanf("%d", &(ps->date[ps->count].sex));
		printf("请输入电话号码:");
		scanf("%s", ps->date[ps->count].telephone_number);
		printf("请输入地址:");
		scanf("%s", ps->date[ps->count].address);

		ps->count++;
		printf("增加成功\n");
	}
	else
		printf("通讯录已满 \n");
}
void Show_AddressList_info(const AddressList* ps)
{
	assert(ps);

	if (!(ps->count))
	{
		printf("Nothing\n");
		return;
	}
	printf("name\tage\tsex\tphone\t\taddress\t\n");
	for (int i = 0; i < ps->count; i++)
	{
		printf("%s\t%3d\t%2d\t%-11s\t%s\t\n", ps->date[i].name, ps->date[i].age, ps->date[i].sex, ps->date[i].telephone_number, ps->date[i].address);
	}
}

static int Find_by_name(const AddressList* ps, const char* name)
{
	assert(ps);
	for (int i = 0; i < ps->count; i++)
	{
		if (strcmp(ps->date[i].name, name) == 0)
			return i;
	}
	return -1;
}

void Del_AddressList_info(AddressList * ps)
{
	assert(ps);
	if (ps->count > 0)
	{
		int index = 0;
		char name[20] = { 0 };
		printf("请输入要删除人的名字：");
		scanf("%19s", name);
		index = Find_by_name(ps, name);
		if (index >= 0)
		{
			memset(ps->date[index].name, 0, sizeof(ps->date[index].name));
			ps->date[index].age = 0;
			memset(ps->date[index].telephone_number, 0, sizeof(ps->date[index].telephone_number));
			memset(ps->date[index].address, 0, sizeof(ps->date[index].address));
			printf("删除成功\n");
			// 后面的元素向前移动
			/*for (int i = index; i < ps->count - 1; i++)
			{
				strcpy(ps->date[i].name, ps->date[i + 1].name);
				ps->date[i].age = ps->date[i + 1].age;
				ps->date[i].sex = ps->date[i + 1].sex;
				strcpy(ps->date[i].telephone_number, ps->date[i+1].telephone_number);
				strcpy(ps->date[i].address, ps->date[i+1].address);
			}*/
			memmove(&(ps->date[index]), &(ps->date[index + 1]), sizeof(ps->date[index]) * (ps->count - index + 2));
			ps->count--;
		}
		else
			printf("没有该数据,删除失败\n。");
	}
	else
		printf("通讯录为空，无法删除\n");
}


void Change_Address_info(AddressList* ps)
{
	assert(ps);

	if (ps->count > 0)
	{
		int index = 0;
		char name[20];
		int flag = 0;
		printf("请输入要修改的人的姓名：");
		scanf("%19s", name);
		index = Find_by_name(ps, name);
		if (index >= 0)
		{
			do {
				printf("\t1、名字\t2、年龄\t3、性别\t4、电话号码\t5、住址\t0、退出修改\n");
				printf("请输入要修改的信息：");
				scanf("%d", &flag);
				switch (flag)
				{
				case 1:
					printf("请输入要更改的名字：");
					scanf("%19s", ps->date[index].name);
					break;
				case 2:
					printf("请输入要修改的年龄：");
					scanf("%d", &ps->date[index].age);
					break;
				case 3:
					printf("请输入要修改的性别：");
					scanf("%d", &ps->date[index].sex);
					break;
				case 4:
					printf("请输入要修改的电话号码：");
					scanf("%11s", ps->date[index].telephone_number);
					break;
				case 5:
					printf("请输入要修改的地址：");
					scanf("%19s", ps->date[index].address);
					break;
				case 0:
					printf("退出修改\n");
					break;
				default:
					printf("请输入正确值\n");
					break;
				}
			} while (flag);
		}
		else
			printf("没有找到，无法修改\n");
	}
	else
		printf("通讯录为空，无法修改");

}
static print_date(const AddressList* ps, int i)
{
	assert(ps);
	printf("name\tage\tsex\tphone\t\taddress\t\n");
	printf("%s\t%3d\t%2d\t%-11s\t%s\t\n", ps->date[i].name, ps->date[i].age, ps->date[i].sex, ps->date[i].telephone_number, ps->date[i].address);
}
void Search_AddressList_info(const AddressList* ps)
{
	assert(ps);

	if (ps->count >= 0)
	{
		int index = 0;
		char name[20];
		printf("请输入要查询信息的名字: ");
		scanf("%19s", name);
		index = Find_by_name(ps, name);
		if (index >= 0)
		{
			print_date(ps, index);
		}
		else
		{
			printf("没有找到\n");
		}
	}
	else
		printf("通讯录为空，不能查询\n");
}
static int cmp_by_name_up(const void * s1,const void * s2)
{
	Pinfo* c1 = (Pinfo*)s1;
	Pinfo* c2 = (Pinfo*)s2;
	return strcmp(c1->name, c2->name);
}
static int cmp_by_name_down(const void * s1,const void * s2)
{
	Pinfo* c1 = (Pinfo*)s1;
	Pinfo* c2 = (Pinfo*)s2;
	return strcmp(c2->name, c1->name);
}
void Sort_AddressList_info(AddressList* ps)
{
	assert(ps);

	if (ps->count >= 0)
	{
		int flag = 0;
		printf("1、a-z\t-1、z-a\t0、退出排序\n");
		printf("请输入序号: ");
		scanf("%d", &flag);
		if (flag == 1)
		{
			// 库函数快速排序（通用版）
			qsort(ps->date, ps->count , sizeof(ps->date[0]), cmp_by_name_up);
			printf("升序排列\n");
		}
		else if (flag == -1) 
		{
			qsort(ps->date, ps->count , sizeof(ps->date[0]), cmp_by_name_down);
			printf("降序排列\n");
		}
		else if (flag == 0)
			printf("退出排序\n");
	}
	else
		printf("通讯录为空\n");
}